package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.MVQosStrategyAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname MVQosStrategyAttrLinkHandler
 * @Description MPLS-VPN产品 QoS策略信息 相关属性联动
 * @Author zhang.song
 * @Date 2021-03-11 16:32
 */
@Service
@AttrHandler(MVQosStrategyAttrHandler.class)
public class MVQosStrategyAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String filedName = attr.getFieldName();
        String value = attr.getValue();
        switch (filedName) {
            case "2_0000037":
                // QOS策略=三级QOS，标准资费类型=显示
                KiteAttr attr1 = inst.getAttr("200001726");
                if(KiteStringUtils.equals("1_0000209", value)) {
                    if (Objects.nonNull(attr1)) {
                        attr1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }

                    setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "2_0000155", "2_0000156", "200001729", "200001731");
                } else {
                    if (Objects.nonNull(attr1)) {
                        attr1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }

                    if(KiteStringUtils.equals("1_0000210", value)) {
                        // QOS策略=五级QOS
                        KiteAttr distributeAttr = inst.getAttr("2_0000059");
                        if (Objects.nonNull(distributeAttr)) {
                            if(KiteStringUtils.equals(distributeAttr.getValue(), "1_0000256")) {
                                // QOS带宽分配方式=速率
                                setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "200001729", "200001731");
                                setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "2_0000155", "2_0000156");
                            } else if(KiteStringUtils.equals(distributeAttr.getValue(), "1_0000257")) {
                                // QOS带宽分配方式=百分比
                                setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "2_0000155", "2_0000156");
                                setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "200001729", "200001731");
                            }
                        }
                    }
                }
                break;
            case "2_0000059":
                KiteAttr strategyAttr = inst.getAttr("2_0000037");
                if(KiteStringUtils.equals("1_0000257", value)) {
                    // QOS带宽分配方式=百分比，QOS白金百分比、QOS金百分比、QOS银百分比 显示
                    setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "2_0000152", "2_0000153", "2_0000154");
                    setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "2_0000038", "2_0000039", "2_0000040", "200001729", "200001731");
                    if (Objects.nonNull(strategyAttr)) {
                        if(KiteStringUtils.equals(strategyAttr.getValue(), "1_0000210")) {
                            // QOS策略=五级QOS，QOS金+百分比、QOS银+百分比 显示
                            setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "2_0000155", "2_0000156");
                        } else {
                            setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "2_0000155", "2_0000156");
                        }
                    }
                } else {
                    // QOS带宽分配方式=速率，QOS白金速率、QOS金速率、QOS银速率
                    setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "2_0000038", "2_0000039", "2_0000040");
                    setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "2_0000152", "2_0000153", "2_0000154", "2_0000155", "2_0000156");
                    if (Objects.nonNull(strategyAttr)) {
                        if(KiteStringUtils.equals(strategyAttr.getValue(), "1_0000210")) {
                            // QOS策略=五级QOS，QOS金+速率、QOS银+速率 显示
                            setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_D, "200001729", "200001731");
                        } else {
                            setAttrShowType(inst, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "200001729", "200001731");
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    public static void setAttrShowType(KiteInst inst, String showType, String... filedNames) {
        for (String filedName : filedNames) {
            KiteAttr attr = inst.getAttr(filedName);
            attr.setShowType(showType);
        }
    }
}
